package com.ldl.origami.origamiService;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.ldl.origami.dao.TutorialMapper;
import com.ldl.origami.domain.Tutorial;
import com.ldl.origami.util.InitServlet;
import com.ldl.origami.util.OrigamiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:
 * 作者: LDL
 * 创建时间: 2014/7/15 15:09
 */
@Service
public class RankingService {

    static Logger logger = LoggerFactory.getLogger(RankingService.class);

    @Autowired(required = false)
    private TutorialMapper tutorialMapper;


    /**
     * 获取热门排行
     * @return
     */
    public List<Tutorial> getHotList(int page) throws Exception {
        PageBounds pageBounds = new PageBounds(page, InitServlet.hotlistLimit, Order.formString(InitServlet.hotlistOrder));
        List<Tutorial> tutorials = null;
        try {
            tutorials = tutorialMapper.selectByPage(pageBounds);
        } catch (Exception e) {
            logger.debug("查询热门排行失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return tutorials;
    }

    /**
     * 获取最新发布排行
     *
     * @return
     */
    public List<Tutorial> getNewList(int page) throws Exception {
        PageBounds pageBounds = new PageBounds(page, InitServlet.newlistLimit, Order.formString(InitServlet.newlistOrder));
        List<Tutorial> tutorials = null;
        try {
            tutorials = tutorialMapper.selectByPage(pageBounds);
        } catch (Exception e) {
            logger.debug("查询最新发布排行失败: "+ OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return tutorials;
    }

    /**
     * 查询前十的用户
     * @return
     * @throws Exception
     */
    public List<String> getTop10Users() throws Exception {
        List<String> users = null;
        try {
            users = tutorialMapper.selectTop10();
        } catch (Exception e) {
            logger.debug("查询用户排行失败: " + OrigamiUtils.getErrorInfoFromException(e));
            throw new Exception();
        }
        return users;
    }
}
